package com.jcl.recruit.crawler.mapper;

import com.jcl.recruit.crawler.model.CrawlerDownloadTask;
import org.apache.ibatis.annotations.*;

import java.util.Date;
import java.util.List;

/**
 * @ClassName CrawlerDownloadTaskMapper
 * @Description TODO
 * @Author yyliu
 * @Date 2018/10/22 14:27
 * @Version 1.0
 **/
public interface CrawlerDownloadTaskMapper {

    @Select({
            "<script>",
            "select * from crawler_download_task ",
            "<where>",
            "<if test=\"null != corpCode\">",
             " and corp_code = #{corpCode}",
            "</if>",
            "<if test=\"null != channelType\">",
            " and channel_type = #{channelType}",
            "</if>",
            "<if test=\"null != resumeId\">",
            " and resume_id = #{resumeId} ",
            "</if>",
            "<if test=\"null != status\">",
            " and status = #{status} ",
            "</if>",
            "</where>",
            " order by create_time asc",
            "</script>"
    })
    List<CrawlerDownloadTask> findList(@Param("corpCode") String corpCode,
                                       @Param("channelType") String channelType,
                                       @Param("resumeId") Integer resumeId,
                                       @Param("status") Integer status);

    @Insert({
            "insert into crawler_download_task ",
            "(user_id, resume_id, corp_code, channel_type, external_msg, status, create_time) ",
            "values ( ",
            "#{userId, jdbcType=INTEGER}, #{resumeId, jdbcType=INTEGER}, #{corpCode, jdbcType=VARCHAR}, ",
            "#{channelType, jdbcType=VARCHAR}, #{externalMsg, jdbcType=VARCHAR}, ",
            "#{status, jdbcType=INTEGER}, #{createTime, jdbcType=TIMESTAMP}",
            ")"
    })
    @Options(useGeneratedKeys = true, keyProperty = "id")
    int add(CrawlerDownloadTask crawlerDownloadTask);

    @Update({
            "<script>",
            " update crawler_download_task set status = #{status} where resume_id in ",
            "<foreach collection=\"resumeIds\" item=\"resumeId\" open=\"(\" close=\")\" separator=\",\"> " ,
            " #{resumeId}",
            "</foreach> ",
            "</script>"
    })
    int updateByResumeIds(@Param("resumeIds") List<Integer> resumeIds, @Param("status") Integer status);

    @Update({
            "<script>",
            " update crawler_download_task set status = #{status} where id in ",
            "<foreach collection=\"ids\" item=\"id\" open=\"(\" close=\")\" separator=\",\"> " ,
            " #{id}",
            "</foreach> ",
            "</script>"
    })
    int update(@Param("ids") List<Integer> ids, @Param("status") Integer status);

    @Update({
            "<script>",
            " update crawler_download_task set update_time = #{date} " ,
                    "where corp_code = #{corpCode} and resume_id = #{resumeId} ",
            "</script>"
    })
    int updateTimer(@Param("date") String date, @Param("corpCode") String corpCode, @Param("resumeId") String resumeId);
}
